<template>
  <div>
    <n-modal
      v-model:show="showModal"
      :mask-closable="false"
      :show-icon="false"
      preset="dialog"
      transform-origin="center"
      title="绑定会员"
      :style="{
        width: dialogWidth,
      }"
    >
      <n-scrollbar style="max-height: 87vh" class="pr-5">
        <n-spin :show="loading" description="请稍候...">
          <n-form
            ref="formRef"
            :model="formValue"
            :label-placement="settingStore.isMobile ? 'top' : 'left'"
            :label-width="100"
            class="py-4"
          >
            <n-grid cols="1 s:1 m:1 l:1 xl:1 2xl:1" responsive="screen">
              <n-gi span="1">
                <n-form-item label="会员" path="memberNo">
                  <n-select
                    v-model:value="formValue.pmsMemberId"
                    :options="memberList"
                    label-field="memberNo"
                    value-field="id"
                    :render-label="renderMemberLabel"
                    filterable
                    :filter="filterMethod"
                    placeholder="请输入会员编号/会员名称/会员手机进行搜索"
                    @search="handleSearch"
                    :show="selectShow"
                    @update:value="handleUpdate"
                  />
                </n-form-item>
              </n-gi>
            </n-grid>
          </n-form>
        </n-spin>
      </n-scrollbar>
      <template #action>
        <n-space>
          <n-button @click="closeForm">
            取消
          </n-button>
          <n-button type="info" :loading="formBtnLoading" @click="confirmForm">
            确定
          </n-button>
        </n-space>
      </template>
    </n-modal>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed } from 'vue';
import { Bind, View } from '@/api/pmsChannel';
import { renderMemberLabel, State, newState } from './model';
import { useProjectSettingStore } from '@/store/modules/projectSetting';
import { useMessage } from 'naive-ui';
import { adaModalWidth } from '@/utils/hotgo';
import {SelectList} from "@/api/pmsMember/index";

const emit = defineEmits(['reloadTable']);
const message = useMessage();
const settingStore = useProjectSettingStore();
const loading = ref(false);
const showModal = ref(false);
const formValue = ref<State>(newState(null));
const formRef = ref<any>({});
const formBtnLoading = ref(false);
const dialogWidth = computed(() => {
  return adaModalWidth(840);
});
const memberList = ref([]);
const selectShow = ref(false);
const filterMethod = (search, option) => {
  return true;
};

function openModal(state: State) {
  selectShow.value = false
  showModal.value = true;

  // 编辑
  loading.value = true;

  View({ id: state.id })
    .then((res) => {
      if(res.pmsMemberId == 0){
        res.pmsMemberId = null;
      }
      formValue.value = res;
    })
    .finally(() => {
      loading.value = false;
    });
}

function handleSearch(value){
  if(value){
    getMemberList(value)
  }else{
    selectShow.value = false
  }
}

function handleUpdate(){
  selectShow.value = false
}

function getMemberList(keyword){
  SelectList({
    keyword
  }).then((res) => {
    memberList.value = res.list;
    selectShow.value = true
  });
}

function confirmForm(e) {
  e.preventDefault();
  formBtnLoading.value = true;
  formRef.value.validate((errors) => {
    if (!errors) {
      Bind(formValue.value).then((_res) => {
        message.success('操作成功');
        setTimeout(() => {
          closeForm();
          emit('reloadTable');
        });
      });
    } else {
      message.error('请填写完整信息');
    }
    formBtnLoading.value = false;
  });
}

function closeForm() {
  showModal.value = false;
  loading.value = false;
}

defineExpose({
  openModal,
});
</script>

<style lang="less"></style>


